package project;
import java.util.ArrayList;
import java.sql.*;
import java.io.*;

public class RecordsChanged {
private static ArrayList<RecordInfo> changes = new ArrayList<RecordInfo>();
private static int clashes;
public RecordInfo getRecord(int i){
    return changes.get(i);
}
public void addRecord(RecordInfo rci){
    for (int i=0;i<changes.size();i++){
        if (rci.getTableName().equalsIgnoreCase(changes.get(i).getTableName())
                && rci.getEntryNum() == changes.get(i).getEntryNum()){
            clashes++;
        }
    }

    changes.add(rci);
        System.out.println("Changes: "+ changes.size() + ", Clashes: "+ clashes);
}
public void resolveClashes(){
    if (clashes != 0){
        int clashesFound = 0;
        try{
        for (int i=0;i<changes.size();i++){
            for (int j=i;j<changes.size();j++){
                if (changes.get(j).getTableName().equalsIgnoreCase(changes.get(i).getTableName())
                && changes.get(j).getEntryNum() == changes.get(i).getEntryNum()
                        && changes.get(j).getDatabase() != changes.get(i).getDatabase()){
                    System.out.println("i"+i+" j"+j+" entry"+changes.get(i).getEntryNum());
                    clashesFound++;
                    ClashScreen cs = new ClashScreen(changes.get(i), changes.get(j));
                       changes.remove(i);
                       changes.remove(j);
                       j=i;
                       changes.add(cs.getResult());
                    if (clashesFound >= clashes){
                        i = changes.size()+1;
                        throw new Exception();
                    }
                }
            }
        }
        } catch (Exception e){

        }
    }

}
public void performUpdates(){
    DatabaseInfo dbLocal = null;
    try {

           Class.forName("com.mysql.jdbc.Driver").newInstance();
	} catch (Exception e) {
            System.err.println("Error loading Driver");
	}
        try {
            FileInputStream fis = new FileInputStream("local.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            dbLocal = (DatabaseInfo)ois.readObject();
            ois.close();fis.close();
        } catch (IOException e){
            System.err.println(e.getMessage());
        } catch (ClassNotFoundException e){
            System.err.println(e.getMessage());
        }
        Connection conn = null;
        try{
            conn = DriverManager.getConnection("jdbc:mysql://" + dbLocal.getHostName()+":"+dbLocal.getPortNo()+"/"+dbLocal.getDatabaseName(),
            dbLocal.getUserName(),dbLocal.getPassword());
            conn.setAutoCommit(false);
            Statement stmt = conn.createStatement();
            for (int i=0;i<changes.size();i++){
                ResultSet fieldNames = conn.getMetaData().getColumns(null, null, changes.get(i).getTableName(), null);
                int j=0;
                while (fieldNames.next()){
                     System.out.print("fields: " + fieldNames.getString(4));
                     System.out.println(" = "+ changes.get(i).getAttribute(j).toString());
                    int x = stmt.executeUpdate("update "+ changes.get(i).getTableName()+ " set "+fieldNames.getString(4)+
                            " = \"" + (changes.get(i).getAttribute(j).toString())+ "\" where id = "+changes.get(i).getEntryNum());
                j++;
                }
            }
            conn.commit();
            System.out.println("Synchronisation Successful");
            System.exit(1);
        } catch (SQLException e){
            System.err.println(e.getMessage());
            try{
            conn.rollback();
            System.out.println("Synchronisation Unsuccessful");
            System.exit(1);
            } catch (Exception ex){
                System.err.println(e.getMessage());
            }
        } catch (Exception e){
            System.err.println(e.getMessage());
        }

}
}
